<template>
  <el-dialog :title="form.id ? '查看' : '新增'" v-model="visible" :close-on-click-modal="false" draggable>
    <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
      <el-row :gutter="24">
        <el-col :span="12" class="mb20">
          <el-form-item label="工号:" prop="code">
            <div>{{ form.code }}</div>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label="姓名:" prop="name">
            <div>{{ form.name }}</div>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label="岗位:" prop="post">
            <div>{{ form.post }}</div>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="联系方式:" prop="phone">
            <div>{{ form.phone }}</div>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label="留言时间:" prop="messageTime">
            <div>{{ form.messageTime }}</div>
          </el-form-item>
        </el-col>
        <!-- 
        <el-col :span="12" class="mb20">
          <el-form-item label="是否采纳" prop="isAccept">
            <el-input v-model="form.isAccept" placeholder="请输入是否采纳" />
          </el-form-item>
        </el-col> -->
        <el-col :span="12" class="mb20">
          <el-form-item label="留言类型:" prop="type">
            <dict-tag :options="cply_type" :value="form.type"></dict-tag>
          </el-form-item>
        </el-col>

        <el-col :span="24" class="mb20">
          <div style="margin: 20px;">详细描述</div>
          <div style="background-color: #f0f0f0;width: 100%;border-radius: 5px;padding: 10px;margin: 20px">{{
            form.description }}
          </div>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer" v-if="form.isAccept == '0'">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="onSubmit" :disabled="loading">采纳</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="ProductMessageDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from "/@/hooks/message";
import { getObj, addObj, putObj } from '/@/api/appinfo/productmessage'
import { rule } from '/@/utils/validate';
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false)
const loading = ref(false)
// 定义字典
const { pm_accept, cply_type } = useDict('pm_accept', 'cply_type')

// 提交表单数据
const form = reactive({
  id: '',
  code: '',
  name: '',
  post: '',
  job: '',
  type: '',
  description: '',
  phone: '',
  messageTime: '',
  isAccept: '',
});

// 定义校验规则
const dataRules = ref({

})

// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true
  form.id = ''

  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });

  // 获取productMessage信息
  if (id) {
    form.id = id
    getproductMessageData(id)
  }
};

// 提交
const onSubmit = async () => {
  const valid = await dataFormRef.value.validate().catch(() => { });
  if (!valid) return false;

  try {
    loading.value = true;
    form.isAccept = '1'
    form.id ? await putObj(form) : await addObj(form);
    useMessage().success(form.id ? '修改成功' : '添加成功');
    visible.value = false;
    emit('refresh');
  } catch (err: any) {
    useMessage().error(err.msg);
  } finally {
    loading.value = false;
  }
};


// 初始化表单数据
const getproductMessageData = (id: string) => {
  // 获取数据
  loading.value = true
  getObj(id).then((res: any) => {
    Object.assign(form, res.data)
  }).finally(() => {
    loading.value = false
  })
};

// 暴露变量
defineExpose({
  openDialog
});
</script>